// Decompiled by GOOSIN CO,LTD Copyright 2010 Pavel Kouznetsov.
// home page: http://www.Goosin.net
// Decompiler options: packimports(3) fieldsfirst ansi space 
// Source File Name:   transferGather_garbage_ok.java

package draft.stock;

import include.nseer_cookie.*;
import include.nseer_db.nseer_db_backup1;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.StringTokenizer;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import javax.servlet.jsp.JspFactory;
import validata.*;

public class transferGather_garbage_ok extends HttpServlet
{

	public transferGather_garbage_ok()
	{
	}

	public synchronized void service(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException
	{
		HttpSession dbSession = request.getSession();
		JspFactory _jspxFactory = JspFactory.getDefaultFactory();
		javax.servlet.jsp.PageContext pageContext = _jspxFactory.getPageContext(this, request, response, "", true, 8192, true);
		javax.servlet.ServletContext dbApplication = dbSession.getServletContext();
		HttpSession session = request.getSession();
		nseer_db_backup1 stock_db = new nseer_db_backup1(dbApplication);
		ValidataNumber validata = new ValidataNumber();
		counter count = new counter(dbApplication);
		ValidataTag vt = new ValidataTag();
		ValidataRecord vr = new ValidataRecord();
		try
		{
			if (stock_db.conn((String)dbSession.getAttribute("unit_db_name")))
			{
				String gather_ID = request.getParameter("gather_ID");
				String product_amount = request.getParameter("product_amount");
				String config_id = request.getParameter("config_id");
				String checker_ID = (String)session.getAttribute("human_IDD");
				int num = Integer.parseInt(product_amount);
				String reasonexact = request.getParameter("reasonexact");
				StringTokenizer tokenTO = null;
				String stock_ID = "";
				String stock_name = "";
				if (!reasonexact.equals("  /"))
				{
					for (tokenTO = new StringTokenizer(reasonexact, "/"); tokenTO.hasMoreTokens();)
					{
						stock_ID = tokenTO.nextToken();
						stock_name = tokenTO.nextToken();
					}

				} else
				{
					tokenTO = new StringTokenizer(reasonexact, "/");
					if (tokenTO.hasMoreTokens())
						stock_ID = tokenTO.nextToken();
				}
				String register = request.getParameter("register");
				String register_time = request.getParameter("register_time");
				String check_time = request.getParameter("check_time");
				String checker = request.getParameter("checker");
				String bodyc = new String(request.getParameter("remark").getBytes("UTF-8"), "UTF-8");
				String remark = exchange.toHtml(bodyc);
				int p = 0;
				for (int i = 1; i <= num; i++)
				{
					String tem_amount = (new StringBuilder("amount")).append(i).toString();
					String amount = request.getParameter(tem_amount);
					if (amount.equals(""))
						amount = "0";
					if (!validata.validata(amount))
						p++;
				}

				if (vt.validata((String)dbSession.getAttribute("unit_db_name"), "stock_transfer_gather", "gather_ID", gather_ID, "check_tag").equals("5"))
				{
					if (p == 0)
					{
						String time = "";
						Date now = new Date();
						SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
						time = formatter.format(now);
						String stock_gather_ID = NseerId.getId("stock/gather", (String)dbSession.getAttribute("unit_db_name"));
						String stock_pay_ID = NseerId.getId("stock/pay", (String)dbSession.getAttribute("unit_db_name"));
						try
						{
							double demand_amount = 0.0D;
							double cost_price_sum = 0.0D;
							for (int i = 1; i <= num; i++)
							{
								String tem_product_name = (new StringBuilder("product_name")).append(i).toString();
								String tem_product_ID = (new StringBuilder("product_ID")).append(i).toString();
								String tem_amount = (new StringBuilder("amount")).append(i).toString();
								String tem_cost_price = (new StringBuilder("cost_price")).append(i).toString();
								String product_name = request.getParameter(tem_product_name);
								String product_ID = request.getParameter(tem_product_ID);
								String amount = request.getParameter(tem_amount);
								if (amount.equals(""))
									amount = "0";
								String cost_price2 = request.getParameter(tem_cost_price);
								StringTokenizer tokenTO3 = new StringTokenizer(cost_price2, ",");
								String cost_price;
								String cost_price1;
								for (cost_price = ""; tokenTO3.hasMoreTokens(); cost_price = (new StringBuilder(String.valueOf(cost_price))).append(cost_price1).toString())
									cost_price1 = tokenTO3.nextToken();

								double subtotal = Double.parseDouble(cost_price) * Double.parseDouble(amount);
								cost_price_sum += subtotal;
								demand_amount += Double.parseDouble(amount);
								String sql1 = (new StringBuilder("update stock_transfer_gather_details set product_ID='")).append(product_ID).append("',product_name='").append(product_name).append("',amount='").append(amount).append("',cost_price='").append(cost_price).append("',subtotal='").append(subtotal).append("' where gather_ID='").append(gather_ID).append("' and details_number='").append(i).append("'").toString();
								stock_db.executeUpdate(sql1);
							}

							String sql = (new StringBuilder("update stock_transfer_gather set reasonexact='")).append(reasonexact).append("',register='").append(register).append("',register_time='").append(register_time).append("',check_time='").append(check_time).append("',checker='").append(checker).append("',remark='").append(remark).append("',demand_amount='").append(demand_amount).append("',cost_price_sum='").append(cost_price_sum).append("',check_tag='2' where gather_ID='").append(gather_ID).append("'").toString();
							stock_db.executeUpdate(sql);
						}
						catch (Exception ex)
						{
							ex.printStackTrace();
						}
						response.sendRedirect("draft/stock/transferGather_ok.jsp?finished_tag=3");
					} else
					{
						response.sendRedirect("draft/stock/transferGather_ok.jsp?finished_tag=1");
					}
				} else
				{
					response.sendRedirect("draft/stock/transferGather_ok.jsp?finished_tag=4");
				}
				stock_db.commit();
				stock_db.close();
			} else
			{
				response.sendRedirect("error_conn.htm");
			}
		}
		catch (Exception ex)
		{
			ex.printStackTrace();
		}
	}
}
